package cn.itcast.bos.web.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {

	// 定义一个日志显示
	private static final Logger LOGGER = LoggerFactory.getLogger(WayBillAction.class);

	@Autowired
	private WayBillService wayBillService;

	// 在快速编辑运单页面的分页查询
	@Action(value = "waybill_pageQuery", results = {
			@Result(name = "success", type = "json") })
	public String pageQuery() {
		//无条件查询,添加了排序的查询
		Pageable pageable = new PageRequest(page - 1, rows,
				new Sort(new Sort.Order(Sort.Direction.DESC, "id")));
		// 调用业务层进行分页查询,要传入模型model
		Page<WayBill> page = wayBillService.pageQuery(model,pageable);

		// 将返回值封装并压入值栈的操作
		pushPageDataToValueStack(page);
		return SUCCESS;
	}

	// 在编辑完成后执行onAfterEdit事件中的保存请求
	@Action(value = "waybill_save",
			results = { @Result(name = "success", type = "json") })
	/*
	 * @Action(value = "waybill_save", results = { @Result(name = "success",
	 * type = "redirect", location = "./pages/take_delivery/waybill_quick.html")
	 * })
	 */
	public String save() {
		Map<String, Object> map = new HashMap<String, Object>();

		LOGGER.info("订单" + model.getOrder());
		try {
			// 判断Waybill对象中的order的id属性
			if (model.getOrder() != null && (model.getOrder().getId() == null
					|| model.getOrder().getId() == 0)) {
				// 此时的Order对象不为null而其id为null,处于瞬时态,这时保存WayBill对象将会报错
				// 解决方法是将瞬时态对象置为null或先对该瞬时态对象进行保存操作使被session管理(有Id)
				model.setOrder(null);
			}

			// 调用业务层进行保存的操作
			wayBillService.save(model);
			// 保存成功,封装结果数据返回给页面
			map.put("success", true);
			map.put("msg", "运单保存成功!");
			LOGGER.info("运单保存成功,运单号: " + model.getWayBillNum());
		} catch (Exception e) {
			e.printStackTrace();
			// 保存失败,封装失败的相关数据返回页面
			map.put("success", false);
			map.put("msg", "运单保存失败!");
			LOGGER.info("运单保存失败,运单号: " + model.getWayBillNum());
		}

		// 将结果数据压入值栈
		ActionContext.getContext().getValueStack().push(map);

		return SUCCESS;
	}

	// 运单输入中利用快速运单号进行回显
	@Action(value = "waybill_findByWayBillNum",
			results = { @Result(name = "success", type = "json") })
	public String findByWayBillNum() {
		WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
		Map<String, Object> map = new HashMap<String, Object>();
		if (wayBill == null) {
			// 运单不存在
			map.put("success", false);
		} else {
			// 运单查询成功
			map.put("success", true);
			map.put("wayBillData", wayBill);
		}

		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}

}
